package cup.common.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author : ruoyu
 * @date : 2022/10/18
 */
public class PageUtil {


    /**
     * 所有list 手动分页
     */
    public static <T> Page<T> toPageByList(List<T> list, Integer page, Integer pageSize) {
        Page<T> pages = new Page<>(page, pageSize);
        if (CollectionUtils.isEmpty(list)) {
            return pages;
        }
        int p = page * pageSize;
        List<T> ts = list.subList(p - (page == 1 ? p : pageSize), Math.min(list.size(), p));
        pages.setRecords(ts);
        pages.setTotal(list.size());
        return pages;
    }
}
